import java.util.Stack;

public class Sort {
    /**
     * 快速排序
     *      时间复杂度
     *          最好:O(N*log(2)N)
     *          最坏:O(N^2)
     *      空间复杂度
     *          最好:O(log(2)N)
     *          最坏:O(N)
     *      稳定性:不稳定
     */
    public static void quickSort(int[] array) {
        quick(array,0,array.length - 1);
    }
    private static void quick(int[] array,int start,int end) {
        if(start > end) {
            return;
        }
        //使用快排
        if(end - start + 1 <= 19) {
            insertSort(array,start,end);
            return;
        }
        //三数取中
        int index = middleNum(array,start,end);
        swap(array,index,start);

        int pivot = partition(array,start,end);
        //左
        quick(array,start,pivot - 1);
        //右
        quick(array,pivot + 1,end);
    }

    /**
     * 快排
     * @param array
     * @param left
     * @param right
     */
    private static void insertSort(int[] array,int left,int right) {
        for(int i = left + 1;i <= right;i++) {
            int tmp = array[i];
            int j = i - 1;
            for(;j >= left;j--) {
                if(array[j] > tmp) {
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }
    /**
     * 取中间元素
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int middleNum(int[] array,int left,int right) {
        int mid = left + (right - left) / 2;
        if(array[left] < array[right]) {
            if(array[mid] > array[right]) {
                return right;
            }else if(array[mid] < array[left]) {
                return left;
            }else {
                return mid;
            }
        }else {
            if(array[mid] > array[left]) {
                return left;
            }else if(array[mid] < array[right]) {
                return right;
            }else {
                return mid;
            }
        }
    }
    /**
     * 查找分割位置
     * @param array
     * @param start
     * @param end
     * @return
     */
    private static int partitionHoare(int[] array,int start,int end) {
        int left = start;
        int right = end;
        while(left < right) {
            //找小于值(先)
            while(left < right && array[right] >= array[start]) {
                right--;
            }
            //找大于值
            while(left < right && array[left] <= array[start]) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,start,left);
        return left;
    }
    private static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    /**
     * 挖坑法
     */
    private static int partition(int[] array,int start,int end) {
        int left = start;
        int right = end;
        int tmp = array[start];
        while(left < right) {
            //找小于值要先
            while(left < right && array[right] >= tmp) {
                right--;
            }
            if(left >= right) {
                break;
            }
            array[left] = array[right];
            while(left < right && array[left] <= tmp) {
                left++;
            }
            if(left >= right) {
                break;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
    /**
     * 快速排序(非递归)
     */
    public static void quickSortNor(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length - 1;
        int pivot = partition(array,left,right);

        if(pivot - 1 > left) {
            stack.push(left);
            stack.push(pivot - 1);
        }

        if(pivot + 1 < right) {
            stack.push(pivot + 1);
            stack.push(right);
        }

        while(!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            pivot = partition(array,left,right);

            if(pivot - 1 > left) {
                stack.push(left);
                stack.push(pivot - 1);
            }

            if(pivot + 1 < right) {
                stack.push(pivot + 1);
                stack.push(right);
            }
        }
    }
    /**
     * 归并排序
     *      时间复杂度:O(N * log(2)N)
     *      空间复杂度:O(N)
     *      稳定性:稳定
     */
    public static void mergeSort(int[] array) {
        mergeFunc(array,0,array.length - 1);
    }

    /**
     * 分解
     * @param array
     * @param left
     * @param right
     */
    private static void mergeFunc(int[] array,int left,int right) {
        if(left >= right) {
            return ;
        }
        int mid = left + (right - left) / 2;
        //左
        mergeFunc(array,left,mid);
        //右
        mergeFunc(array,mid + 1,right);
        //合并
        merge(array,left,mid,right);
    }

    /**
     * 合并
     * @param array
     * @param left
     * @param mid
     * @param right
     */
    private static void merge(int[] array,int left,int mid,int right) {
        int start1 = left;
        int end1 = mid;
        int start2 = mid + 1;
        int end2 = right;

        int[] tmpArrays = new int[right - left + 1];
        int k = 0;

        while(start1 <= end1 && start2 <= end2) {
            if(array[start1] <= array[start2]) {
                tmpArrays[k++] = array[start1++];
            }else  {
                tmpArrays[k++] = array[start2++];
            }
        }
        while(start1 <= end1) {
            tmpArrays[k++]  = array[start1++];
        }
        while(start2 <= end2) {
            tmpArrays[k++]  = array[start2++];
        }

        //拷贝
        for(int i = 0;i < tmpArrays.length;i++) {
            array[i + left] = tmpArrays[i];
        }
    }
    /**
     * 归并排序(非递归)
     */
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while(gap < array.length) {
            for(int i = 0;i < array.length;i = i + 2 * gap) {
                int left = i;
                int mid = i + gap - 1;
                if(mid >= array.length) {
                    mid = array.length - 1;
                }
                int right = mid + gap;
                if(right >= array.length) {
                    right = array.length - 1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }
    /**
     * 计数排序
     *      时间复杂度:O(范围+N)
     *      空间复杂度:O(N)
     *      稳定性:稳定
     */
    public static void countSort(int[] array) {
        int min = 0;
        int max = 0;
        //找最值
        for(int i = 1;i < array.length;i++) {
            if(array[i] < array[min]) {
                min = i;
            }
            if(array[i] > array[max]) {
                max = i;
            }
        }
        //计数
        int result = array[max] - array[min];
        int[] count = new int[result + 1];
        for(int i = 0;i < array.length;i++) {
            int index = array[i] - array[min];
                count[index]++;
        }
        int tmp = array[min];
        //遍历count
        int k = 0;
        for(int i = 0;i < count.length;i++) {
            while(count[i] > 0) {
                array[k++] = i + tmp;
                count[i]--;
            }
        }
    }
}
